﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace WJ.Infrastructure.Util
{
    public class Security
    {
        #region AES加密
        /// <summary>
        /// AES加密
        /// </summary>
        /// <param name="toEncrypt"></param>
        /// <returns></returns>
        public static string AESEncrypt(string toEncrypt, string key)
        {
            //if (string.IsNullOrWhiteSpace(toEncrypt))
            //    return string.Empty;
            //// 256-AES key    
            //byte[] keyArray = Encoding.Default.GetBytes(key);
            //byte[] toEncryptArray = Encoding.Default.GetBytes(toEncrypt);

            //RijndaelManaged rDel = new RijndaelManaged();
            //rDel.Key = keyArray;
            //rDel.Mode = CipherMode.ECB;
            //rDel.Padding = PaddingMode.PKCS7;

            //ICryptoTransform cTransform = rDel.CreateEncryptor();
            //byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            //return Convert.ToBase64String(resultArray);

            if (string.IsNullOrWhiteSpace(toEncrypt))
                return string.Empty;
            // 256-AES key    
            byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);
            byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);

            RijndaelManaged rDel = new RijndaelManaged();
            rDel.Key = keyArray;
            rDel.Mode = CipherMode.ECB;
            rDel.Padding = PaddingMode.PKCS7;

            ICryptoTransform cTransform = rDel.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);


            return Convert.ToBase64String(resultArray, 0, resultArray.Length);

        }
        #endregion

        #region AES解密
        /// <summary>
        /// AES解密
        /// </summary>
        /// <param name="toDecrypt"></param>
        /// <returns></returns>
        public static string Decrypt(string toDecrypt, string key)
        {
            //if (string.IsNullOrWhiteSpace(toDecrypt))
            //    return string.Empty;
            //try
            //{
            //    // 256-AES key    
            //    byte[] keyArray = Encoding.Default.GetBytes(key);
            //    byte[] toEncryptArray = Encoding.Default.GetBytes(toDecrypt);

            //    RijndaelManaged rDel = new RijndaelManaged();
            //    rDel.Key = keyArray;
            //    rDel.Mode = CipherMode.ECB;
            //    rDel.Padding = PaddingMode.PKCS7;

            //    ICryptoTransform cTransform = rDel.CreateDecryptor();

            //    byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            //    return Convert.ToBase64String(resultArray);
            //}
            //catch { return toDecrypt; }

            if (string.IsNullOrWhiteSpace(toDecrypt))
                return string.Empty;
            try
            {
                // 256-AES key    
                byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);
                byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);

                RijndaelManaged rDel = new RijndaelManaged();
                rDel.Key = keyArray;
                rDel.Mode = CipherMode.ECB;
                rDel.Padding = PaddingMode.PKCS7;

                ICryptoTransform cTransform = rDel.CreateDecryptor();

    
                byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
                return UTF8Encoding.UTF8.GetString(resultArray);
            }
            catch { return toDecrypt; }
        }
        #endregion

        #region MD5加密
        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string GetMd5Hash(string input)
        {
            MD5 md5Hash = MD5.Create();

            // Convert the input string to a byte array and compute the hash.
            byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

            // Create a new Stringbuilder to collect the bytes
            // and create a string.
            StringBuilder sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data 
            // and format each one as a hexadecimal string.
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            // Return the hexadecimal string.
            return sBuilder.ToString();
        }
        #endregion

    }
}
